package com.um.jdy.app.shop.manager;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.um.jdy.app.activity.manager.ActivityManager;
import com.um.jdy.app.activity.entity.dto.FullReduceGoodsPageDTO;
import com.um.jdy.app.activity.entity.dto.SeckillGoodsDTO;
import com.um.jdy.app.activity.entity.vo.SeckillTimesVO;
import com.um.jdy.common.shop.enums.DiyEnum;
import com.um.jdy.app.shop.enums.GoodsRecommendType;
import com.um.jdy.common.shop.entity.po.Diy;
import com.um.jdy.common.shop.service.DiyService;
import com.um.springboot.starter.utils.JSONUtil;
import com.um.springboot.starter.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @Title
 * @Description
 * @Author renjunwei
 * @Date 2023/5/22 10:37
 */
@Slf4j
@Service
public class DiyManager {

    @Autowired
    ActivityManager activityManager;

    @Autowired
    GoodsManager goodsManager;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    DiyService diyService;

    /**
     * 查询首页
     *
     * @return
     */
    public Map getHome() {
        String redisTemplateKey = DiyEnum.Type.Home.getValue() + "-" + diyService.getComId();
        Map pageData = redisTemplate.opsForHash().entries(redisTemplateKey);
        if (null == pageData || pageData.isEmpty()) {
            pageData = this.getDiyData(DiyEnum.Type.Home.getValue());
            redisTemplate.opsForHash().putAll(redisTemplateKey, pageData);
            redisTemplate.expireAt(redisTemplateKey, DateUtil.endOfHour(DateUtil.date()));
        } else {
            //更新不需要缓存的数据
            this.updateDiyData(pageData);
        }
        return pageData;
    }

    private Map getDiyData(String type){
        Diy diy = diyService.getEntity(new String[]{"type", "pro_name"}, new String[]{"diy", type});
        Map pageData = JSONUtil.parseJSONToMap(diy.getProValue());
        List<Map> componentList = (List<Map>)pageData.get("items");

        for (Map component : componentList) {

            switch (StringUtil.getTrim(component.get("component_name"))){
                case "GroupData":
                    component.put("data_items",activityManager.getGroupGoodsList(StringUtil.toInt(component.get("goods_num"))));
                    break;

                case "SeckillData":
                    List<SeckillTimesVO> seckillTimesList = activityManager.getSeckillTimesList();
                    if(seckillTimesList.size() > 0){
                        component.put("seckill_list",seckillTimesList);
                        String marketSeckillTimesId = "";
                        for (SeckillTimesVO seckillTimes : seckillTimesList){
                            if(DateUtil.compare(DateUtil.parse(seckillTimes.getEndTime()), new Date()) > 0){
                                marketSeckillTimesId = seckillTimes.getMarketSeckillTimesId();
                                seckillTimes.setIsDefaultShow(true);
                                break;
                            }
                        }
                        SeckillGoodsDTO dto = new SeckillGoodsDTO();
                        dto.setMarket_seckill_times_id(StrUtil.isEmpty(marketSeckillTimesId)?seckillTimesList.get(0).getMarketSeckillTimesId():marketSeckillTimesId);
                        component.put("data_items",activityManager.getSeckillGoodsList(dto,StringUtil.toInt(component.get("goods_num"))));
                    }else{
                        component.put("seckill_list",new ArrayList());
                        component.put("data_items",new ArrayList());
                    }
                    break;

                case "FullReductionData":
                    FullReduceGoodsPageDTO page = new FullReduceGoodsPageDTO();
                    page.setPage_no(1);
                    page.setPage_size(StringUtil.toInt(component.get("goods_num")));
                    component.put("data_items",activityManager.getFullReduceGoodsPage(page).getResult());
                    break;

                case "TopicData":
                    // TOTO 社交相关的不做不做
                    // component.put("data_items",socialTopicService.getList(StringUtil.toInt(component.get("goods_num"))));
                    break;

                case "FootCategoryData":
                    component.put("category_list", GoodsRecommendType.toList());
                    component.put("data_items",goodsManager.getRecommendGoods(GoodsRecommendType.CWBK.getKey(), StringUtil.toInt(component.get("goods_num"))));
                    break;

                case "NewGiftData":
                    // TOTO 赠品卷不做
                    // List<Map> couponsList = (List) component.get("data_items");
                    // for (Map coupons : couponsList){
                    //     if(StringUtil.equals(coupons.get("type"), CouponsType.GIFT.getKey())){
                    //         List goods = dao.queryListBySQLName("get_coupons_goods_list",new Object[]{coupons.get("activity_coupons_id")});
                    //         coupons.put("goods",goods);
                    //     }
                    // }
                    // break;
            }

        }

        pageData.put("items",componentList);
        return pageData;
    }

    private Map updateDiyData(Map pageData){
        List<Map> componentList = (List<Map>)pageData.get("items");

        for (Map component : componentList) {

            switch (StringUtil.getTrim(component.get("component_name"))){

                case "SeckillData":
                    //修改缓存数据为正在进行的秒杀场次
                    List<SeckillTimesVO> seckillTimesList = (List<SeckillTimesVO>) component.get("seckill_list");
                    if(seckillTimesList.size() > 0){
                        String marketSeckillTimesId = "";
                        for (SeckillTimesVO seckillTimes : seckillTimesList){
                            if(DateUtil.compare(DateUtil.parse(seckillTimes.getEndTime()),new Date()) > 0){
                                marketSeckillTimesId = seckillTimes.getMarketSeckillTimesId();
                                seckillTimes.setIsDefaultShow(true);
                                break;
                            }
                        }
                        SeckillGoodsDTO dto = new SeckillGoodsDTO();
                        dto.setMarket_seckill_times_id(StrUtil.isEmpty(marketSeckillTimesId)?seckillTimesList.get(0).getMarketSeckillTimesId():marketSeckillTimesId);
                        component.put("data_items",activityManager.getSeckillGoodsList(dto,StringUtil.toInt(component.get("goods_num"))));
                    }
                    break;

            }

        }

        pageData.put("items",componentList);
        return pageData;
    }

}
